import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yyshort/common/model/video_model.dart';

import '../index.dart';

class SearchResultWidget extends GetView<SearchResultController> {
  final ValueChanged<VideoModel>? onPlayTap;

  const SearchResultWidget({super.key, this.onPlayTap});

  @override
  Widget build(BuildContext context) {
    return GetX<SearchResultController>(
      builder: (controller) {
        late bool enableRefresh = controller.state.isSearch.value;
        print("resultList长度：${controller.state.resultList.length}");

        return SmartRefresher(
          enablePullDown: enableRefresh, // 禁用下拉刷新
          enablePullUp: enableRefresh,
          controller: controller.refreshController,
          onRefresh: controller.onRefresh,
          onLoading: controller.onLoading,
          child: CustomScrollView(
            slivers: [
              // 推荐项（如果有）
              // SliverPadding(
              //   padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 5.w),
              //   sliver: SliverToBoxAdapter(child: _buildRecommendItem()),
              // ),

              // 搜索结果列表
              SliverPadding(
                padding: EdgeInsets.symmetric(
                  vertical: 10.w,
                  horizontal: 23.5.w,
                ),
                sliver: SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      var item = enableRefresh
                          ? controller.state.resultList[index]
                          : controller.state.foryouList[index];
                      return _buildSearchResultItem(item);
                    },
                    childCount: enableRefresh
                        ? controller.state.resultList.length
                        : controller.state.foryouList.length,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  // 处理点击事件
  void _onItemTapped(VideoModel item) {
    if (onPlayTap != null) {
      onPlayTap!(item);
    }
  }

  // 构建搜索结果项
  Widget _buildSearchResultItem(VideoModel item) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10.w),
      height: (121 + 20).h,
      // color: Colors.amber,
      child: Stack(
        children: [
          Positioned(
            left: 15.w,
            top: 2.h,
            child: Container(
              width: 91.w,
              height: 127.h,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6.r),
                color: Colors.black,
                boxShadow: [
                  BoxShadow(
                    // ignore: deprecated_member_use
                    color: Colors.orange.withOpacity(0.3),
                    offset: Offset(0, 1.h),
                    blurRadius: 6.r,
                  ),
                ],
              ),
            ),
          ),

          Container(
            margin: EdgeInsets.only(top: 20.w),
            child: InkWell(
              onTap: () => _onItemTapped(item),
              borderRadius: BorderRadius.circular(6.r),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Colors.black,
                  boxShadow: [
                    BoxShadow(
                      // ignore: deprecated_member_use
                      color: Colors.orange.withOpacity(0.3),
                      offset: Offset(0, 2.h),
                      blurRadius: 6.r,
                    ),
                  ],
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(6.r),
                  child: Container(
                    color: Color(0xFF1C1C1C), //#1C1C1C
                    padding: EdgeInsets.fromLTRB(15, 12, 15, 15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(width: 91.w, height: 127.h),

                        SizedBox(width: 13.5.w),

                        // 右侧文本区域
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              // 标题文本
                              Text(
                                item.name ?? '',
                                style: TextStyle(
                                  fontFamily: 'PingFangSC',
                                  fontSize: (30 / 2).w,
                                  color: Colors.white,
                                  height: 1.2,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),

                              SizedBox(height: (16 / 2).w),

                              // 描述文本
                              Text(
                                item.description ?? '',
                                style: TextStyle(
                                  fontFamily: 'PingFangSC-Regular',
                                  fontWeight: FontWeight.w400,
                                  fontSize: (24 / 2).w,
                                  color: Colors.white,
                                  letterSpacing: 0,
                                  height: (28 / 2) / (24 / 2),
                                ),
                                softWrap: true,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),

                              SizedBox(height: (16 / 2).w),
                              Visibility(
                                visible: item.tags?.isNotEmpty ?? false
                                    ? true
                                    : false,
                                child: // 标签按钮
                                Row(
                                  children: [
                                    Container(
                                      width: (148 / 2).w,
                                      height: (32 / 2).w,
                                      decoration: BoxDecoration(
                                        color: const Color(0xFFFFB42C),
                                        borderRadius: BorderRadius.circular(
                                          (16 / 2).w,
                                        ),
                                      ),
                                      child: Center(
                                        child: Text(
                                          item.tags?.isNotEmpty ?? false
                                              ? item.tags![0]
                                              : 'Tag1',
                                          style: TextStyle(
                                            color: Colors.black,
                                            fontSize: (16 / 2).w,
                                            fontWeight: FontWeight.w500,
                                          ),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                      ),
                                    ),

                                    SizedBox(width: (10 / 2).w),

                                    // 第二个标签
                                    if ((item.tags?.length ?? 0) > 1)
                                      Container(
                                        width: (148 / 2).w,
                                        height: (32 / 2).w,
                                        decoration: BoxDecoration(
                                          color: const Color(0xFFFFB42C),
                                          borderRadius: BorderRadius.circular(
                                            (16 / 2).w,
                                          ),
                                        ),
                                        child: Center(
                                          child: Text(
                                            item.tags![1],
                                            style: TextStyle(
                                              color: Colors.black,
                                              fontSize: (16 / 2).w,
                                              fontWeight: FontWeight.w500,
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                      ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
          Positioned(
            left: 15.w,
            top: 2.h,
            child: // 图片组件
            GestureDetector(
              onTap: () {
                _onItemTapped(item);
              },
              child: ClipRRect(
                borderRadius: BorderRadius.circular(6.r),
                child: CachedNetworkImage(
                  imageUrl: item.imgUrl ?? '',
                  width: 91.w,
                  height: 127.h,
                  fit: BoxFit.cover,
                  errorWidget: (context, url, error) => Icon(Icons.error),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
